//
//  LoginLeftViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/13.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "LoginLeftViewController.h"
#import "MobilePhoneQuickLoginViewController.h"
#import "Masonry.h"
#import "RegisterViewController.h"
#import "NetworkHandler.h"
#import "NSString+LSTMD5.h"
#import "LoginSonViewController.h"
@interface LoginLeftViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *textFieldID;
@property (nonatomic, strong)UITextField *textFieldPassWorld;
@property (nonatomic, strong)NSMutableDictionary *dic;
@end

@implementation LoginLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatSubView];
}
- (void)creatSubView {
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    backImage.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backImage];
    UIImageView *logoImage = [[UIImageView alloc] init];
    [backImage addSubview:logoImage];
    [logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(self.view.frame.size.width / 2);
        make.height.mas_equalTo(self.view.bounds.size.height * 0.15);
        make.top.mas_equalTo(self.view.frame.size.height * 0.15);
    }];
    self.textFieldID = [[UITextField alloc] init];
    _textFieldID.backgroundColor = [UIColor yellowColor];
    _textFieldID.placeholder = @"请输入手机号";
    _textFieldID.delegate = self;
    [self.view addSubview:_textFieldID];
    [_textFieldID mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logoImage.mas_bottom).offset(35);
        make.width.mas_equalTo(backImage.frame.size.width - 30);
        make.left.mas_equalTo (15);
        make.height.mas_equalTo(35);
    }];
    self.textFieldPassWorld = [[UITextField alloc] init];
    _textFieldPassWorld.backgroundColor = [UIColor yellowColor];
    _textFieldPassWorld.placeholder = @"请输入密码";
    _textFieldPassWorld.delegate = self;
    [self.view addSubview:_textFieldPassWorld];
    [_textFieldPassWorld mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldID.mas_bottom).offset(10);
        make.width.mas_equalTo(_textFieldID);
        make.left.mas_equalTo(_textFieldID);
        make.height.mas_equalTo(35);
    }];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitle:@"手机快捷登录" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickPhoneBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_textFieldPassWorld);
        make.top.mas_equalTo(_textFieldPassWorld.mas_bottom).offset(5);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(120);
    }];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"忘记密码" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backImage addSubview:btn2];
    [btn2 addTarget:self action:@selector(clickForgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(5);
        make.width.mas_equalTo(btn1);
        make.height.mas_equalTo(btn1);
        make.top.mas_equalTo(_textFieldPassWorld.mas_bottom).offset(5);
    }];
    UIButton *Loginbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    Loginbtn.backgroundColor = [UIColor cyanColor];
    [Loginbtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:Loginbtn];
    [Loginbtn addTarget:self action:@selector(clickLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    [Loginbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(btn1.mas_bottom).offset(20);
        make.width.mas_equalTo(_textFieldID);
        make.left.mas_equalTo(_textFieldID);
        make.height.mas_equalTo(50);
    }];
    UIButton *RegisterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [RegisterBtn setTitle:@"注册" forState:UIControlStateNormal];
    RegisterBtn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:RegisterBtn];
    [RegisterBtn addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    [RegisterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Loginbtn.mas_bottom).offset(20);
        make.width.height.mas_equalTo(Loginbtn);
        make.left.mas_equalTo(Loginbtn);
    }];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"使用以下方式登录";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:12];
    [backImage addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo (10);
        make.width.mas_equalTo (self.view.frame.size.width / 3);
        make.top.mas_equalTo(RegisterBtn.mas_bottom).offset(40);
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor whiteColor];
    [backImage addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(label.mas_width);
        make.height.mas_equalTo(1);
        make.centerY.mas_equalTo(label.mas_centerY);
        make.right.mas_equalTo(label.mas_left).offset(10);
    }];
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor whiteColor];
    [backImage addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(view1);
        make.height.mas_equalTo(view1);
        make.centerY.mas_equalTo(label.mas_centerY);
        make.left.mas_equalTo(label.mas_right).offset(10);
    }];
    UIButton *QQbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    QQbtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:QQbtn];
    [QQbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(35);
        make.top.mas_equalTo(view1.mas_bottom).offset(20);
        make.centerX.mas_equalTo(view1.mas_centerX);
    }];
    UIButton *WBbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    WBbtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:WBbtn];
    [WBbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(QQbtn);
        make.top.mas_equalTo(QQbtn);
        make.centerX.mas_equalTo(label.mas_centerX);
    }];
    UIButton *WXbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    WXbtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:WXbtn];
    [WXbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(QQbtn);
        make.top.mas_equalTo(QQbtn);
        make.centerX.mas_equalTo(view2.mas_centerX);
    }];
}
- (void)clickPhoneBtn:(UIButton *)sender {
    MobilePhoneQuickLoginViewController *vc = [[MobilePhoneQuickLoginViewController alloc] init];
    vc.title = sender.titleLabel.text;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)clickForgetPassword:(UIButton *)sender {
    MobilePhoneQuickLoginViewController *vc = [[MobilePhoneQuickLoginViewController alloc] init];
    vc.title = sender.titleLabel.text;
    [self.navigationController pushViewController:vc animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_textFieldID resignFirstResponder];
    [_textFieldPassWorld resignFirstResponder];
    return YES;
}
// 登录按钮
- (void)clickLoginBtn:(UIButton *)sender {
    if (_textFieldID.text != nil && _textFieldPassWorld.text != nil) {
        [self creatLoginHandData];
           }
}
- (void)creatLoginHandData {
    NSString *strUrl = @"http://api.guju.com.cn/v2/user/authorization";
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval timeInterVal = [date timeIntervalSince1970];
    // 时间戳
    NSString *strTimer = [NSString stringWithFormat:@"%.0f", timeInterVal];
    // 对密码进行MD5 加密
    NSString *str32 = [_textFieldPassWorld.text stringWith32BitMD5Lower];
    NSString *strBody = [NSString stringWithFormat:@"datestamp=%@&password=%@&type=1&user=%@", strTimer, str32, _textFieldID.text];
    // 发送请求
    [NetworkHandler getDataByUrlString:strUrl BodyString:strBody WithDataBlock:^(id result) {
       
        NSString *datestamp = [result objectForKey:@"datestamp"];
        if (datestamp) {
            // 存在NSUserDefaults里
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:result forKey:@"dic"];
            LoginSonViewController *loginVC = [[LoginSonViewController alloc] init];
            loginVC.hidesBottomBarWhenPushed = NO;
            [self.navigationController pushViewController:loginVC animated:YES];
        }
    }];
}
- (void)clickRegister:(UIButton *)sender {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    LoginSonViewController *loginVC = [[LoginSonViewController alloc] init];
    loginVC.hidesBottomBarWhenPushed = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
